package cn.onein.edu.backend.jms.producer;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.jms.core.MessageCreator;

import cn.onein.edu.backend.job.JobType;
import cn.onein.edu.biz.bean.User;
import cn.onein.edu.common.MailType;
import cn.onein.edu.common.base.JmsSupport;

public class SendMailProducer extends JmsSupport {

	/**
	 * 发送用户每日行动指南邮件
	 * @param user
	 */
	public void sendDailyMailMessage(final User user){
		getJmsTemplate().send(getDestination(), new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				Message message = session.createMessage();

				// 设置邮件类型
				message.setStringProperty(MSG_KEY_SEND_MAIL_TYPE, MailType.DailyAction.toString());
				// 设置用户ID
				message.setStringProperty(MSG_KEY_USER_ID, user.getId().toString());
				// 设置用户Email
				message.setStringProperty(MSG_KEY_USER_EMAIL, user.getEmail());
				// 设置用户名
				message.setStringProperty(MSG_KEY_USER_NAME,user.getName());
				// 设置用户昵称
				message.setStringProperty(MSG_KEY_USER_NICK_NAME,user.getNickName());
				return message;
			}
		});
	}

	/**
	 * 处理Job时的监控邮件邮件发送消息
	 * 
	 * @param mailType
	 * @param jobType
	 * @param userEmail
	 */
	public void sendJobMailMessage(final MailType mailType,
			final JobType jobType, final String userEmail) {
		sendJobMailMessage(mailType,jobType,userEmail,"无");
	}

	/**
	 * 处理Job时的监控邮件邮件发送消息
	 * 
	 * @param mailType
	 * @param jobType
	 * @param userEmail
	 * @param jobMessage
	 */
	public void sendJobMailMessage(final MailType mailType,
			final JobType jobType, final String userEmail,
			final String jobMessage) {
		if (userEmail == null || "".equals(userEmail.trim())) {
			throw new IllegalArgumentException(
					"处理Job时的监控邮件邮件发送消息时，用户email地址不能为空！");
		}

		getJmsTemplate().send(getDestination(), new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {

				Message message = session.createMessage();

				// 设置邮件类型
				message.setStringProperty(MSG_KEY_SEND_MAIL_TYPE, mailType.toString());
				// 设置JOB类型
				message.setStringProperty(MSG_KEY_JOB_TYPE, jobType.toString());
				// 设置用户email地址
				message.setStringProperty(MSG_KEY_USER_EMAIL, userEmail);
				// 设置Job消息
				message.setStringProperty(MSG_KEY_JOB_MESSAGE, jobMessage);

				return message;
			}
		});
	}
}
